package com.anyi.blog.service.Impl;

import com.anyi.blog.entity.User;
import com.anyi.blog.mapper.UserMapper;
import com.anyi.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        return userMapper.login(username,password);
    }

    @Override
    public User register(User request) {
        userMapper.register(request);
        return userMapper.SelectByusername(request.getUsername());

    }

    @Override
    public UserDetails selectByUsername(String username) {
        User user =  userMapper.SelectByusername(username);
        return new UserDetails() {
            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                return List.of();
            }

            @Override
            public String getPassword() {
                return user.getPassword();
            }

            @Override
            public String getUsername() {
                return user.getUsername();
            }
        };
    }
}
